package servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import service.AdminService;
import util.MD5;

public class ManageAdminServlet extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public ManageAdminServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doPost(request, response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to
	 * post.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html");
		PrintWriter out = response.getWriter();

		String action = request.getParameter("action");
		String userName = request.getParameter("userName");
		String passWord = request.getParameter("passWord");
		String mailBox = request.getParameter("mailBox");
		String phoneNum = request.getParameter("phoneNum");
		String adminType = request.getParameter("adminType");
		
		String result = "failed";
		if (action.equals("add")) {
			try {
				if (AdminService.checkAdminByName(userName)) {
					out.print("nameAlreadyHave");
					return;
				}
				if (AdminService.checkAdminByEmail(mailBox)) {
					out.print("mailBoxAlreadyHave");
					return;
				}
				if (AdminService.checkAdminByPhone(phoneNum)) {
					out.print("phoneAlreadyHave");
					return;
				}

				result = AdminService.creatAdmin(userName,
						MD5.Encrypt(passWord), mailBox, phoneNum, adminType);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		
		else if (action.equals("change")) {
			String adminId = request.getParameter("adminId");
			try {
				if (AdminService.checkChangeByName(userName)) {
					out.print("nameAlreadyHave");
					return;
				}
				if (AdminService.checkChangeByEmail(mailBox)) {
					out.print("mailBoxAlreadyHave");
					return;
				}
				if (AdminService.checkChangeByPhone(phoneNum)) {
					out.print("phoneAlreadyHave");
					return;
				}

				result = AdminService.changeAdmin(userName, MD5.Encrypt(passWord), mailBox, phoneNum, adminType, adminId);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		else if(action.equals("delete"))
		{
			String idStr = "("+request.getParameter("idStr")+')';
			System.out.println(idStr);
			try {
				AdminService.deleteAdmin(idStr);
				result = "success";
			} catch (SQLException e) {
				e.printStackTrace();
			}
			
		}
		

		out.print(result);

		System.out.println(action + "---" + userName + "----" + passWord
				+ "----" + mailBox + "----" + phoneNum + "----" + adminType);
		out.flush();
		out.close();
	}

	/**
	 * Initialization of the servlet. <br>
	 * 
	 * @throws ServletException
	 *             if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
